package com.webdrider.servlet.util.parser.snippet;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractResponse;
import com.common.security.exceptions.HTTPException;
import com.common.security.exceptions.ParserException;
import com.google.appengine.api.datastore.Text;
import com.webdrider.model.action.snippet.Article;
import com.webdrider.model.action.snippet.Domain;
import com.webdrider.model.action.snippet.Tag;
import com.webdrider.model.requests.snippet.AddSnippetArticleRequest;
import com.webdrider.model.response.snippet.AddSnippetArticleResponse;
import com.webdrider.servlet.abstracts.XMLParserBase;
import com.webdrider.servlet.util.HTTPProtocol;

public class AddSnippetArticleXMLParser extends XMLParserBase {

	@Override
	public AbstractRequest parseRequest(InputStream stream) throws ParserException, HTTPException {
		AddSnippetArticleRequest request = new AddSnippetArticleRequest();
		
		Document document = createDocument(stream);
		
		Node actionNode = getActionNode(document);
		Node contentNode = getContentNode(document);
		
		insertAction(actionNode, request);
		Element element = getContent(contentNode);
		
		Element session = getElement(element, HTTPProtocol.SESSION);
		Element article = getElement(element, HTTPProtocol.ARTICLE);
		List<Element> tags = getAllElements(article, HTTPProtocol.TAG);
		Article articleValue = new Article();

		Text value = new Text(article.getAttribute(HTTPProtocol.ATTRIBUTE_VALUE));
		Domain domain = new Domain(article.getAttribute(HTTPProtocol.ATTRIBUTE_DOMAIN));
		String title = article.getAttribute(HTTPProtocol.ATTRIBUTE_TITLE);
		List<Tag> tagValues = new ArrayList<Tag>();
		
		for (int i = 0; i < tags.size(); i++) {
			tagValues.add(new Tag(tags.get(i).getAttribute(HTTPProtocol.ATTRIBUTE_VALUE)));
		}
		
		articleValue.setContent(value);
		articleValue.setDomain(domain);
		articleValue.setTitle(title);
		articleValue.setTags(tagValues);
		
		request.setSessionId(session.getAttribute(HTTPProtocol.ATTRIBUTE_VALUE));
		request.setArticle(articleValue);
		
		return request;
	}

	@Override
	public String parseResponse(AbstractResponse abstractResponse) throws ParserException {
		AddSnippetArticleResponse response = null;
		
		if (abstractResponse instanceof AddSnippetArticleResponse) {
			response = (AddSnippetArticleResponse) abstractResponse;
		} else {
			throw new ParserException(ParserException.RESPONSE_MATCH_ERROR);
		}
		
		StringBuilder builder = new StringBuilder();
		
		builder.append(HTTPProtocol.RESPONSE_TAG);
		builder.append(HTTPProtocol.CLOSE_TAG);
		builder.append(HTTPProtocol.END_LINE);
		builder.append(getAction(response.getRequestAction()));
		builder.append(HTTPProtocol.CONTENT_TAG);
		builder.append(HTTPProtocol.END_LINE);
		builder.append(HTTPProtocol.CONTENT_END_TAG);
		builder.append(HTTPProtocol.END_LINE);
		builder.append(HTTPProtocol.RESPONSE_END_TAG);
		
		return builder.toString();
	}

}
